 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=124 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Consider data layer tools
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='Castor,JDBC,JDO,ModelBuilder,MyUserDAOSql,TopLink,database,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Consider data layer tools</div>

<div class='main-body'>
 
<br>The standard JDBC API is rather low level. Many applications will benefit
from using tools that operate at a higher level of abstraction.
<p>Examples of such tools include :
<ul>
<li>the <a href="http://java.sun.com/javaee/overview/faq/persistence.jsp">Java Persistence API</a></li>

<li><a href="http://www.hibernate.org/">Hibernate</a></li>

<li>the <a href="TopicAction5f31-2.html">WEB4J</a> data layer</li>
</ul>

The <a href="TopicAction5f31-2.html">WEB4J</a> data layer differs from other tools
in significant ways :
<ul>
<li>
no explicit mapping of the database structure is necessary</li>

<li>
all interaction with the database is through existing SQL, not some parallel
tool (such as Hibernate Query Language)</li>

<li>
SQL statements remain in their natural form, in plain text files, and are
not placed in classes or XML files.</li>

<li>
to translate a <tt>ResultSet</tt> into a <a href="TopicActiond08d-2.html">Model
Object</a>, an <i>ordering</i> convention is used instead of a <i>naming</i>
convention</li>

<li>
Model Objects can be simple and <a href="TopicActiond838-2.html">immutable</a>, whereas
other tools usually require Model Objects to follow the <a href="TopicActionfbbc-2.html">Java
Beans anti-pattern</a></li>
</ul>
Here is an example of a <a href="TopicAction564b-2.html">Data Access Object</a> implemented
with web4j. The actual SQL statements are placed in <tt>.sql</tt> files placed "near" the corresponding DAO.
Entries in these <tt>.sql</tt> files are identified using <tt>SqlId</tt> members such as <tt>RATING_LIST</tt>.
<br>
<PRE>

<span class='keyword'>package</span> hirondelle.fish.main.rating;

<span class='keyword'>import</span> java.util.List;
<span class='keyword'>import</span> hirondelle.web4j.database.Db;
<span class='keyword'>import</span> hirondelle.web4j.database.DAOException;
<span class='keyword'>import</span> hirondelle.web4j.database.SqlId;
<span class='keyword'>import</span> hirondelle.web4j.model.Id;

<span class='comment'>/** Data Access Object for {@link Rating} objects. */</span>
<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> RatingDAO {

  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>final</span> SqlId RATING_LIST = <span class='keyword'>new</span> SqlId(<span class='literal'>"RATING_LIST"</span>);
  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>final</span> SqlId RATING_FETCH_FOR_CHANGE = <span class='keyword'>new</span> SqlId(<span class='literal'>"RATING_FETCH_FOR_CHANGE"</span>);
  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>final</span> SqlId RATING_CHANGE = <span class='keyword'>new</span> SqlId(<span class='literal'>"RATING_CHANGE"</span>);
  
  <span class='comment'>/** Return a &lt;tt&gt;List&lt;/tt&gt; of all {@link Rating} objects, for all restaurants.  */</span>
  List&lt;Rating&gt; list() <span class='keyword'>throws</span> DAOException {
    <span class='keyword'>return</span> Db.list(Rating.<span class='keyword'>class</span>, RATING_LIST);
  }
  
  <span class='comment'>/** Return a single {@link Rating} identified by its id.  */</span>
  Rating fetch(Id aRatingId) <span class='keyword'>throws</span> DAOException {
    <span class='keyword'>return</span> Db.fetch(Rating.<span class='keyword'>class</span>, RATING_FETCH_FOR_CHANGE, aRatingId);
  }
  
  <span class='comment'>/** Update an existing {@link Rating}. */</span>
  <span class='keyword'>int</span> change(Rating aRating) <span class='keyword'>throws</span> DAOException {
    Object[] params = { 
      aRating.getFishRating(), aRating.getChipsRating(), aRating.getPriceRating(), 
      aRating.getLocationRating(), aRating.getServiceRating(), 
      aRating.getBeerRating(), aRating.getId() 
    };
    <span class='keyword'>return</span> Db.edit(RATING_CHANGE, params);
  }
}
 
</PRE>
<br>
<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicAction2dbf-2.html?Id=184'>Simplify database operations</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='124'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=124 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
